package com.cyan.app.workflow.customview;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cyan.app.workflow.R;

/**
 * Created by DXW on 2016/2/29.
 */
public class ShowCustomDialog extends Dialog {

    private Context mContext;

    public ShowCustomDialog(Context context) {
        super(context);
        mContext = context;
    }

    /**主题的构造*/
    public ShowCustomDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
    }

    public void show() {
        if(mContext == null){
            return;
        }
        if(mContext instanceof Activity){
            if(((Activity)mContext).isFinishing()){
                return;
            }
        }

        super.show();
    };

    /**内部类*/
    public static class Builder{

        /**上下文*/
        private Context context;
        /**标题*/
        private String title;
        /**信息*/
        private String message;
        private SpannableStringBuilder spannableMessage;
        /**左面按钮文字*/
        private String positiveButtonText;
        /**又面按钮文字*/
        private String negativeButtonText;
        /**中间面按钮文字*/
        private String  neutralButtonText;
        /**View*/
        private View contentView;
        /** 提示图片 */
        private int imgId = -1;

        private boolean isUseHtmlCode;

        private boolean isUseSpannableString;

        public Builder(Context context) {
            this.context = context;
        }

        private DialogInterface.OnClickListener
                positiveButtonClickListener,
                neutralButtonClickListener,
                negativeButtonClickListener

                        ;
        //imgDeleteButtonClickListener
        private DialogInterface.OnCancelListener
                onCancelListener;

        /**
         * Set the Dialog message from String
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         *
         * @param spannableMessage
         */
        public void setSpannableMessage(SpannableStringBuilder spannableMessage) {
            this.spannableMessage = spannableMessage;
        }

        /**
         *
         * @param useHtml
         */
        public void setHtmlCode(boolean useHtml) {
            this.isUseHtmlCode = useHtml;
        }

        /**
         *
         * @param isUseSpannableString
         */
        public void setIsUseSpannableString(boolean isUseSpannableString) {
            this.isUseSpannableString = isUseSpannableString;
        }

        /**
         * Set the Dialog message from resource
         * @param message
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setMessage(SpannableStringBuilder message) {
            this.spannableMessage = message;
            return this;
        }

        /**
         * Set the Dialog title from resource
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Set a custom content view for the Dialog.
         * If a message is set, the contentView is not
         * added to the Dialog...
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the positive button text and it's listener
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Set the negative button text and it's listener
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Set the Neutral button resource and it's listener
         * @param neutralButtonText
         * @param listener
         * @return
         */
        public Builder setNeutralButton(int neutralButtonText,
                                        DialogInterface.OnClickListener listener) {
            this.neutralButtonText = (String) context
                    .getText(neutralButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * Set the Neutral button text and it's listener
         * @param neutralButtonText
         * @param listener
         * @return
         */
        public Builder setNeutralButton(String neutralButtonText,
                                        DialogInterface.OnClickListener listener) {
            this.neutralButtonText = neutralButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }


	    /*    public Builder setImgDelButton( DialogInterface.OnClickListener listener){
	        	  this.imgDeleteButtonClickListener = listener;
		          return this;
	        }*/

        public Builder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
            this.onCancelListener = onCancelListener;
            return this;
        }

        /**
         * 自定义提示图片
         */
        public Builder setMessageIcon(int resId) {
            this.imgId = resId;
            return this;
        }


        /**自定义*/
        public ShowCustomDialog createCustomDialog() {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final ShowCustomDialog dialog = new ShowCustomDialog(context, R.style.MyDialog);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(true);
            View layout = inflater.inflate(R.layout.jdd_coustom_dialog_layout, null);


            Window dialogWindow=dialog.getWindow();
            DisplayMetrics metrics=new DisplayMetrics();
            ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int width=(int) ((metrics.widthPixels)*0.5);
            WindowManager.LayoutParams layoutParams=dialog.getWindow().getAttributes();
            layoutParams.width=width;
            dialogWindow.setAttributes(layoutParams);

            dialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));

            //自定义 title
            ((TextView) layout.findViewById(R.id.title_custom)).setText(title);


            //ji
            if(onCancelListener != null) {
                dialog.setOnCancelListener(onCancelListener);
            }

            //左面按钮
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton_btn_custom))
                        .setText(positiveButtonText );

                //左面按钮触发事件
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton_btn_custom)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog,
                                    DialogInterface.BUTTON_POSITIVE
                            );

                        }
                    });

                }else {
                    // if no confirm button just set the visibility to GONE
                    layout.findViewById(R.id.positiveButton_btn_custom).setVisibility(
                            View.GONE);
                }
            } else {
                layout.findViewById(R.id.positiveButton_btn_custom).setVisibility(
                        View.GONE);
            }


            //中间面按钮
            if (neutralButtonText != null) {
                ((Button) layout.findViewById(R.id.neutralButton_btn_custom))
                        .setText(neutralButtonText);

                if (neutralButtonClickListener != null) {

                    ((Button) layout.findViewById(R.id.neutralButton_btn_custom)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            neutralButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                        }
                    });

                }else {
                    layout.findViewById(R.id.neutralButton_btn_custom).setVisibility(
                            View.GONE);
                }


            }else {
                layout.findViewById(R.id.neutralButton_btn_custom).setVisibility(
                        View.GONE);
            }

            //右面按钮
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton_btn_custom))
                        .setText(negativeButtonText);

                if (negativeButtonClickListener != null) {

                    ((Button) layout.findViewById(R.id.negativeButton_btn_custom)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });

                }else {
                    layout.findViewById(R.id.negativeButton_btn_custom).setVisibility(
                            View.GONE);
                }


            }else {
                layout.findViewById(R.id.negativeButton_btn_custom).setVisibility(
                        View.GONE);
            }



            //取消按钮
            // if (imgDeleteButtonClickListener != null) {
            ((ImageView) layout.findViewById(R.id.img_dele_custom)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                    //imgDeleteButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                }
            });
               /* }else {
                	  layout.findViewById(R.id.img_dele_custom).setVisibility(
		                        View.GONE);
				}*/

            //信息
            if (message != null) {
                if(isUseSpannableString) {
                    ((TextView) layout.findViewById(
                            R.id.message_custom)).setText(spannableMessage);
                } else {
                    if (isUseHtmlCode) {
                        ((TextView) layout.findViewById(
                                R.id.message_custom)).setText(Html.fromHtml(message));
                    } else {
                        ((TextView) layout.findViewById(
                                R.id.message_custom)).setText(message);
                    }
                }

            }else if (contentView != null) {

                ((LinearLayout) layout.findViewById(R.id.content_custom))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content_custom))
                        .addView(contentView,
                                new ViewGroup.LayoutParams(
                                        ViewGroup.LayoutParams.WRAP_CONTENT,
                                        ViewGroup.LayoutParams.WRAP_CONTENT));

            }

            ImageView iv = (ImageView) layout.findViewById(R.id.image_custom);
            if(imgId > 0) {
                iv.setBackgroundResource(imgId);
                iv.setVisibility(View.VISIBLE);
            } else {
                iv.setVisibility(View.GONE);
            }

            dialog.setContentView(layout);

            return dialog;

        }

    }
}

